"""
ActionChains 中定义了很多鼠标和键盘的操作

ActionChains 是动作链接 ,
调用 ActionChains 时 , 不会立即执行 , 而是将所有的操作 , 按顺序放在一个队列里 ,
当调用 perform() 方法时 , 队列中的事件会依次执行 .

基本用法:
    生成一个动作: action = ActionChains(driver)
    动作添加方法:
        action.method_1()
        action.method_2()
        action.method_3()
        ( 比如 : action.click() , action.move_to_element() , ... )
        ( 当给 action 添加动作的时候 , 动作并不会立即执行 , 而是 action 调用 perform 时 , 才会执行 . )
    最后执行所有动作:
        action.perform()

    动作添加方法时:
        可以链式写法 ,
        也可分步写法 .


"""

from selenium.webdriver import ActionChains, Keys
from selenium.webdriver.common.by import By

from basic.s1_pytest_base import TestBase


class TestActionChains(TestBase):

    # 点击 , 双击 , 右击
    def test_oo1(self):
        self.driver.get('https://sahitest.com/demo/clicks.htm')
        ele_click = self.driver.find_element(By.CSS_SELECTOR, '[value="click me"]')
        ele_double_click = self.driver.find_element(By.CSS_SELECTOR, '[value="dbl click me"]')
        ele_right_click = self.driver.find_element(By.CSS_SELECTOR, '[value="right click me"]')
        ele_clear = self.driver.find_element(By.CSS_SELECTOR, '[value="Clear"]')

        action = ActionChains(self.driver)
        action.click(ele_click)
        action.double_click(ele_double_click).context_click(ele_right_click).click(ele_click)
        action.click(ele_clear)
        action.perform()

    # 移动到某元素:
    def test_oo2(self):
        self.driver.get('https://www.baidu.com')

        ele_tj_briicon = self.driver.find_element(By.CSS_SELECTOR, '[name="tj_briicon"]')
        ele_setting = self.driver.find_element(By.CSS_SELECTOR, '#s-usersetting-top')
        action = ActionChains(self.driver)
        action.move_to_element(ele_tj_briicon)
        action.move_to_element(ele_setting).perform()

    # 将拖拽的某元素:
    def test_oo3(self):
        self.driver.get('https://sahitest.com/demo/dragDropMooTools.htm')

        dragger = self.driver.find_element(By.CSS_SELECTOR, '#dragger')
        dragger_to_1 = self.driver.find_element(By.CSS_SELECTOR, '.item')
        dragger_to_2 = self.driver.find_element(By.CSS_SELECTOR, '.item:nth-child(5)')
        dragger_to_3 = self.driver.find_element(By.CSS_SELECTOR, '.item:nth-child(6)')
        dragger_to_4 = self.driver.find_element(By.CSS_SELECTOR, '.item:nth-child(7)')

        action = ActionChains(self.driver)

        # action.drag_and_drop(dragger, dragger_to_1).pause(0.2)
        # action.drag_and_drop(dragger, dragger_to_2).pause(0.2)
        # action.drag_and_drop(dragger, dragger_to_3).pause(0.2)
        # action.drag_and_drop(dragger, dragger_to_4).pause(0.2).perform()

        # action.click_and_hold(dragger).release(dragger_to_1).pause(0.2)
        # action.click_and_hold(dragger).release(dragger_to_2).pause(0.2)
        # action.click_and_hold(dragger).release(dragger_to_3).pause(0.2)
        # action.click_and_hold(dragger).release(dragger_to_4).perform()

        action.click_and_hold(dragger).move_to_element(dragger_to_1).release().pause(0.2)
        action.click_and_hold(dragger).move_to_element(dragger_to_2).release().pause(0.2)
        action.click_and_hold(dragger).move_to_element(dragger_to_3).release().pause(0.2)
        action.click_and_hold(dragger).move_to_element(dragger_to_4).release().perform()

    # 键盘操作:
    def test_oo4(self):
        self.driver.get('https://sahitest.com/demo/label.htm')

        input_box = self.driver.find_element(By.CSS_SELECTOR, '[type="textbox"]')

        action = ActionChains(self.driver)
        action.send_keys_to_element(input_box, 'username boxx').pause(0.2)
        action.send_keys(Keys.BACKSPACE).pause(0.2)
        action.send_keys(Keys.BACKSPACE).pause(0.2)
        action.send_keys(Keys.BACKSPACE).pause(0.2)
        action.send_keys(Keys.BACKSPACE).pause(0.2)
        action.send_keys(Keys.BACKSPACE).pause(0.2)
        action.perform()
